package com.cennavi.buss;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.minemap.util.DESUtil;
import com.cennavi.minemap.util.DateTimeUtil;
import com.cennavi.minemap.util.InfoUtil;
import com.cennavi.minemap.util.ShellUtil;

public class ExpirtyInterceptor implements HandlerInterceptor {

    private String expiryFile;

    public ExpirtyInterceptor(String expiryFile) {
        this.expiryFile = expiryFile;
    }

    public boolean sendJson(HttpServletResponse response, String errmsg) {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        JSONObject json = new JSONObject();
        try {
            json.put("errcode", -999);
            json.put("errmsg", errmsg);
            writer = response.getWriter();
            writer.print(json.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null)
                writer.close();
        }
        return false;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // System.out.println(request.getContextPath());
        // System.out.println(request.getRequestURI());
        // System.out.println(request.getRequestURI().replace(request.getContextPath(),
        // ""));

        String now = DateTimeUtil.getNowYearMonthDate();
        try {
            // File f = new
            // File(ExpirtyInterceptor.class.getResource(expiryFile).getFile());
            File f = new File(expiryFile);
            System.out.println("keyfile:" + f.getPath());

            String dmidecode = ShellUtil.shell("dmidecode -s system-serial-number").trim();

            if ("Not Specified".equals(dmidecode)) {
                dmidecode = InfoUtil.getMac();
            }

            while (dmidecode.length() < 16) {
                dmidecode = "0" + dmidecode;
            }

            // System.out.println("dmidecode:" + dmidecode);

            String expiry = DESUtil.readDecryptFile(f.getPath());

            // System.out.println("expiry:" + expiry);

            String expiryDate = DESUtil.decrypt(expiry, dmidecode);
            System.out.println("expiryDate:" + expiryDate);
            if (Integer.parseInt(now) <= Integer.parseInt(expiryDate)) {
                System.out.println("");
                return true;
            } else {
                return sendJson(response, "授权已过期。");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sendJson(response, "无法获取授权文件或授权文件有误。");
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }

}
